﻿using MSSClient.Model;
using MSSClient.Utils;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using TestTreeView.Model;

namespace MSSClient.OCR
{
    /// <summary>
    /// ocr识别处理工具类
    /// </summary>
    class OcrUtil
    {
        /// <summary>
        /// 返回膨胀腐蚀的size
        /// </summary>
        /// <param name="dpi">dpi类型</param>
        /// <param name="type">膨胀腐蚀的标识，1-腐蚀,2-膨胀</param>
        /// <returns></returns>
        public static Mat elementBFByDpi(int dpi, int type)
        {
            #region
            Mat elementBF = new Mat();
            if (type == 1)
            {
                if (dpi.Equals(101))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(3, 3));
                else if (dpi.Equals(150))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(4, 4));
                else if (dpi.Equals(200) || dpi.Equals(300))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(5, 5));
                else if (dpi.Equals(400))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(6, 6));
                else if (dpi.Equals(601))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(9, 9));
            }
            if (type == 2)
            {
                if (dpi.Equals(101))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(2, 2));
                else if (dpi.Equals(150))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(4, 4));
                else if (dpi.Equals(200))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(6, 6));
                else if (dpi.Equals(300))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(7, 7));
                else if (dpi.Equals(400))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(10, 10));
                else if (dpi.Equals(601))
                    elementBF = Cv2.GetStructuringElement(MorphShapes.Rect, new OpenCvSharp.Size(11, 11));
            }
            return elementBF;
            #endregion
        }

        public static Bitmap MatToBitmap(Mat mat)
        {
            #region
            if (mat.Depth() != OpenCvSharp.MatType.CV_8U)
                return null;
            if ((int)mat.Step() % 4 == 0)
                return null;
            Bitmap bitmap = new Bitmap(100, 100);
            int widht = mat.Cols;
            int height = mat.Rows;
            int step = (int)mat.Step();
            IntPtr intPtr = (System.IntPtr)mat.Data;

            if (mat.Channels() == 4)
            {
                bitmap = new Bitmap(
                    widht,
                    height,
                    step,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb,
                    intPtr
                   );
            }

            //彩色图像
            if (mat.Channels() == 3)
            {
                bitmap = new Bitmap(
                    widht,
                    height,
                    step,
                    System.Drawing.Imaging.PixelFormat.Format24bppRgb,
                    intPtr
                   );
            }
            //灰度图像
            else if (mat.Channels() == 1)
            {
                bitmap = new Bitmap(
                    widht,
                    height,
                    step,
                    System.Drawing.Imaging.PixelFormat.Format8bppIndexed,
                    intPtr
                   );
            }
            return bitmap;
            #endregion
        }

        public static Mat BitmapToMat(Bitmap bitmap)
        {
            #region
            Mat mat = new Mat();
            BitmapData bitmapData = new BitmapData();
            //锁定Bitmap数据
            bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            //将 bmpImg 的数据指针复制到 cvImg 中，不拷贝数据
            if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)    // 灰度图像
                mat = new Mat(bitmap.Height, bitmap.Width, MatType.CV_8UC1, bitmapData.Scan0);
            if (bitmap.PixelFormat == PixelFormat.Format16bppArgb1555)    // 彩色图像
                mat = new Mat(bitmap.Height, bitmap.Width, MatType.CV_8UC2, bitmapData.Scan0);
            else if (bitmap.PixelFormat == PixelFormat.Format24bppRgb)  // 彩色图像
                mat = new Mat(bitmap.Height, bitmap.Width, MatType.CV_8UC3, bitmapData.Scan0);
            else if (bitmap.PixelFormat.Equals(PixelFormat.Format32bppArgb))  // 彩色图像
                mat = new Mat(bitmap.Height, bitmap.Width, MatType.CV_8UC4, bitmapData.Scan0);

            //解锁Bitmap数据
            bitmap.UnlockBits(bitmapData);
            CommonUtil.ClearMemory();
            return mat;
            #endregion
        }

        public static String DecodeInvoiceTypeByCn(string IndexItem)
        {
            String ContentItem = "";
            //0104普通发票 0101专用发票  0110电子发票
            if (IndexItem.Equals("0101"))
                ContentItem = "专用发票";
            else if (IndexItem.Equals("0104"))
                ContentItem = "普通发票";
            else if (IndexItem.Equals("0110"))
                ContentItem = "电子发票";
            else ContentItem = "发票";
            return ContentItem;
        }

        public static ImagesData SetImagesData(TreeModel treeItem, String ftpSavePath)
        {
            ImagesData imagesDataTemp = new ImagesData();
            imagesDataTemp.ftpPath = ftpSavePath;//存储的ftp路径
            imagesDataTemp.scanTime = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            imagesDataTemp.isIdentifying = treeItem.IsIdentifying;
            if (treeItem.imageOrder != 0 && treeItem.imageScanTime != "")
            {
                imagesDataTemp.imgOrder = treeItem.imageOrder;
                imagesDataTemp.scanTime = treeItem.imageScanTime;
            }
            //发票信息
            imagesDataTemp.invoiceCode = treeItem.InvoiceModelFun.invoiceCode;
            imagesDataTemp.invoiceDate = treeItem.InvoiceModelFun.invoiceDate;
            imagesDataTemp.invoiceNum = treeItem.InvoiceModelFun.invoiceNum;
            imagesDataTemp.invoiceType = OcrUtil.DecodeInvoiceTypeByNum(treeItem.InvoiceModelFun.invoiceType);
            imagesDataTemp.invoiceContent = treeItem.InvoiceModelFun.invoiceContent;
            imagesDataTemp.amount = treeItem.InvoiceModelFun.amount;
            imagesDataTemp.buyerName = treeItem.InvoiceModelFun.buyerName;
            imagesDataTemp.totalAmount = treeItem.InvoiceModelFun.totalAmount;
            imagesDataTemp.checkCode = treeItem.InvoiceModelFun.checkCode;
            imagesDataTemp.sellerName = treeItem.InvoiceModelFun.sellerName;

            return imagesDataTemp;
        }

        public static String DecodeInvoiceTypeByNum(string invoiceType)
        {
            String Type = "";
            //0104普通发票 0101专用发票  0110电子发票
            if (invoiceType.Equals("0101"))
                Type = "1";
            else if (invoiceType.Equals("0104"))
                Type = "2";
            else if (invoiceType.Equals("0110"))
                Type = "3";
            else Type = "4";
            return Type;
        }

        /// <summary>
        /// 根据dpi来返回二维码区域面积大概范围
        /// </summary>
        /// <param name="dpi"></param>
        /// <returns></returns>
        public static List<int> CompareScope(int dpi)
        {
            #region
            List<int> compareScope = new List<int>();
            if (dpi.Equals(101)) { compareScope.Add(5000); compareScope.Add(20000); }//70x70 - 142x142
            else if (dpi.Equals(150)) { compareScope.Add(10000); compareScope.Add(40000); }//100x100 - 200x200
            else if (dpi.Equals(200)) { compareScope.Add(20000); compareScope.Add(90000); }//122x122 - 245x245
            else if (dpi.Equals(300)) { compareScope.Add(35000); compareScope.Add(120000); }//158x158 - 284x284
            else if (dpi.Equals(400)) { compareScope.Add(45000); compareScope.Add(140000); }//173x173 - 347x347
            else if (dpi.Equals(601)) { compareScope.Add(55000); compareScope.Add(160000); }//200x200 - 388x388
            return compareScope;
            #endregion
        }

        /// <summary>
        /// 根据dpi来返回模糊的size
        /// </summary>
        /// <param name="dpi"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static OpenCvSharp.Size BlurMatSizeByDpi(int dpi)
        {
            #region
            if (dpi.Equals(101))
                return new OpenCvSharp.Size(3, 3);
            else if (dpi.Equals(150))
                return new OpenCvSharp.Size(4, 4);
            else if (dpi.Equals(200))
                return new OpenCvSharp.Size(6, 6);
            else if (dpi.Equals(300))
                return new OpenCvSharp.Size(8, 8);
            else if (dpi.Equals(400) || dpi.Equals(601))
                return new OpenCvSharp.Size(9, 9);
            else
                return new OpenCvSharp.Size(7, 7);
            #endregion
        }
    }
}
